import axios from 'axios'
import { ElMessage } from "element-plus";
import router from '../../router'
let Ajax = axios.create({
    baseURL: "http://127.0.0.1:3003/",
    timeout: 50000
})
Ajax.interceptors.response.use(response => {
    // console.log(response.data,'response.data');
    
    if (response?.data?.data?.code == 2) {
        ElMessage(response.data.data.data)
        router.push('/login')
        localStorage.clear()
        return []
    }
    if (response?.data?.data?.code == 0) {
        ElMessage(response.data.data.data)
        router.push('/login')
        localStorage.clear()
        return []
    }
    // console.log(response.data);
    
    return response.data;
}, error => {
    if (error.includes('timeout')) return
    return Promise.reject(error);
});

const get = (url, params) => {
    Ajax.defaults.headers['token'] = localStorage.getItem('token')
    return new Promise((relove, reject) => {
        Ajax.get(url, { params, headers: { responseType: 'arraybuffer'} }).then(res => relove(res)).catch(err => reject(err))
    })
}
const getTIP = (url, params) => {
    Ajax.defaults.headers['token'] = localStorage.getItem('token')
    return new Promise((relove, reject) => {
        Ajax.get(url, { params, responseType: "arraybuffer",headers: { responseType: 'arraybuffer'} }).then(res => relove(res)).catch(err => reject(err))
    })
}
const post = (url, params) => {
    Ajax.defaults.headers['token'] = localStorage.getItem('token')
    return new Promise((relove, reject) => {
        Ajax.post(url, params).then(res => relove(res)).catch(err => reject(err))
    })
}
export default {
    get, post,getTIP
}